package com.ten.mall.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ten.mall.service.ItemOrderService;
import com.ten.mall.service.UserService;
import com.ten.mall.service.YaodianService;
import com.ten.mall.vo.ItemOrder;
import com.ten.mall.vo.User;
import com.ten.mall.vo.Yaodian;

@Controller
public class YaodianController {
	@Autowired
	private YaodianService yaodianService;
	@Autowired
	private UserService userService;
	@Autowired
	private ItemOrderService itemOrderService;
	@RequestMapping("/admin/yaodianList")
	public String yaodianList(@RequestParam(name="start",defaultValue="1") Integer start,
            				  @RequestParam(name="size",defaultValue="10") Integer size,
            				  Model model,String name){
		  List<Yaodian> list =yaodianService.selectByPages(start, size, name);
		  model.addAttribute("list", list);
		  int total=list.size()/10+1;
		  model.addAttribute("page", start);
		  model.addAttribute("total", total);
		return "jsp/yaodianList";
	}
	@RequestMapping("/admin/addYaodian")
	public String addYaodian(Integer id,HttpServletRequest res){
		List<User> salemanlist =userService.selectSalemanPages(null,"销售员");
		res.setAttribute("salemanlist", salemanlist);
		if(id!=null){
			Yaodian app =yaodianService.selectById(id);
			res.setAttribute("app", app);
		}
		return "jsp/addYaodian";
	}
	
	@RequestMapping("/admin/saveYaodian")
	public String addYaodian(Yaodian yao){
		if(yao.getId()==null){
			yaodianService.insertYaodian(yao);
		}else{
			yaodianService.updateYaodian(yao);
		}
		return "redirect:/admin/yaodianList";
	}
	@RequestMapping("/admin/deleteYaodian")
	@ResponseBody
	public String deleteYaodian(Integer id){
		String rs="error";
		try {
			if(id!=null){
				yaodianService.deleteYaodoan(id);
				rs="ok";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return rs;
	}
	@RequestMapping("/phone/yaodian")
	public String listYao(HttpServletRequest res,String orderId,String name) throws Exception{
		List<Yaodian> list=null;
		ItemOrder order =itemOrderService.selectById(orderId);
		Map<String,String> map =new HashMap<>();
		if(StringUtils.isNotBlank(name)){
			String myName="";
			if(StringUtils.isNotBlank(name)){
					byte[] bytes=name.getBytes("ISO-8859-1");
					myName=new String(bytes,"utf-8");
			}
			map.put("goodsId", String.valueOf(order.getGoodsId()));
			map.put("goodsNum", String.valueOf(order.getOrderSum()));
			map.put("name", myName);
			list=yaodianService.selectByKucun(map);
		}else{
			map.put("goodsId", String.valueOf(order.getGoodsId()));
			map.put("goodsNum", String.valueOf(order.getOrderSum()));
			map.put("name", null);
			list=yaodianService.selectByKucun(map);
		}
		 
		res.setAttribute("list", list);
		res.setAttribute("orderId", orderId);
		return "phone/yaodian";
	}
	/**
	 * ajax查询药店
	 * @param res
	 * @param orderId
	 * @param name
	 * @return
	 */
	@RequestMapping("/phone/yaodianAjax")
	@ResponseBody
	public List<Yaodian> selectByName(HttpServletRequest res,String orderId,String name){
		List<Yaodian> list=null;
		ItemOrder order =itemOrderService.selectById(orderId);
		Map<String,String> map =new HashMap<>();
		if(StringUtils.isNotBlank(name)){
				try {
					map.put("goodsId", String.valueOf(order.getGoodsId()));
					map.put("goodsNum", String.valueOf(order.getOrderSum()));
					map.put("name", name);
					list=yaodianService.selectByKucun(map);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
		}
		return list;
	}
	/**
	 * 我的药店
	 * @param request
	 * @return
	 */
	@RequestMapping("/phone/my_yaodian")
	public String myYaodian(HttpServletRequest request){
		User u =(User) request.getSession().getAttribute("u");
		List<Yaodian> list =yaodianService.selectByvo(u.getRealName());
		request.setAttribute("list",list);
		return "phone/my_yaodian";
	}
}
